<!--
  * 时间：2024/10/28
  * 作者：刘岩
  * 描述：网关管理
-->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="110px">
      <el-form-item label="网关名称" prop="getwayName">
        <el-input
          v-model="queryParams.getwayName"
          placeholder="请输入网关名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="网关MAC地址" prop="macAddr">
        <el-input
          v-model="queryParams.macAddr"
          placeholder="请输入网关MAC地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="网关型号" prop="modelNumber">
        <el-input
          v-model="queryParams.modelNumber"
          placeholder="请输入网关型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="网关IP" prop="ip">
        <el-input
          v-model="queryParams.ip"
          placeholder="请输入网关IP"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="网关端口" prop="port">
        <el-input
          v-model="queryParams.port"
          placeholder="请输入网关端口"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="网关子网掩码" prop="subnetMask">
        <el-input
          v-model="queryParams.subnetMask"
          placeholder="请输入网关子网掩码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:getway:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:getway:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:getway:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['system:getway:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="getwayList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="网关ID" align="center" prop="getwayId" />
      <el-table-column label="网关名称" align="center" prop="getwayName" />
      <el-table-column label="网关MAC地址" align="center" prop="macAddr" />
      <el-table-column label="网关型号" align="center" prop="modelNumber" />
      <el-table-column label="网关IP" align="center" prop="ip" />
      <el-table-column label="网关端口" align="center" prop="port" />
      <el-table-column label="网关子网掩码" align="center" prop="subnetMask" />
      <el-table-column label="网关状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="备注" align="center" prop="remark" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:getway:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:getway:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改网关对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="网关名称" prop="getwayName">
          <el-input v-model="form.getwayName" placeholder="请输入网关名称" />
        </el-form-item>
        <el-form-item label="网关MAC地址" prop="macAddr">
          <el-input v-model="form.macAddr" placeholder="请输入网关MAC地址" />
        </el-form-item>
        <el-form-item label="网关型号" prop="modelNumber">
          <el-input v-model="form.modelNumber" placeholder="请输入网关型号" />
        </el-form-item>
        <el-form-item label="网关IP" prop="ip">
          <el-input v-model="form.ip" placeholder="请输入网关IP" />
        </el-form-item>
        <el-form-item label="网关端口" prop="port">
          <el-input v-model="form.port" placeholder="请输入网关端口" />
        </el-form-item>
        <el-form-item label="网关子网掩码" prop="subnetMask">
          <el-input v-model="form.subnetMask" placeholder="请输入网关子网掩码" />
        </el-form-item>
        <el-form-item label="网关状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
<!--        <el-form-item label="备注" prop="remark">-->
<!--          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGetway, getGetway, delGetway, addGetway, updateGetway } from "@/api/getway";

export default {
  name: "Getway",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 网关表格数据
      getwayList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tenantId: null,
        getwayName: null,
        macAddr: null,
        modelNumber: null,
        ip: null,
        port: null,
        subnetMask: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        getwayName: [
          { required: true, message: '网关名称不能为空', trigger: 'blur' }
        ],
        macAddr: [
          { required: true, message: '网关MAC地址不能为空', trigger: 'blur' }
        ],
        modelNumber: [
          { required: true, message: '网关型号不能为空', trigger: 'blur' }
        ],
        ip: [
          { required: true, message: '网关IP不能为空', trigger: 'blur' }
        ],
        port: [
          { required: true, message: '网关端口不能为空', trigger: 'blur' }
        ],
        subnetMask: [
          { required: true, message: '网关子网掩码不能为空', trigger: 'blur' }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询网关列表 */
    getList() {
      this.loading = true;
      listGetway({
        ...this.queryParams,
        tenantId: this.$store.getters.tenantId
      }).then(response => {
        this.getwayList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        getwayId: null,
        tenantId: null,
        getwayName: null,
        macAddr: null,
        modelNumber: null,
        ip: null,
        port: null,
        subnetMask: null,
        status: "0",
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.getwayId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加网关";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const getwayId = row.getwayId || this.ids
      getGetway(getwayId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改网关";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.getwayId != null) {
            updateGetway(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGetway({
              ...this.form,
              tenantId: this.$store.getters.tenantId
            }).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const getwayIds = row.getwayId || this.ids;
      this.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + getwayIds + '"的数据项？').then(function() {
        return delGetway(getwayIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/getway/export', {
        ...this.queryParams
      }, `getway_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped lang="scss">
.el-select--medium {
  width: 100%;
}
</style>
